package org.gp.Interfaz;

import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.Font;
import javax.swing.JButton;
import java.awt.BorderLayout;
import javax.swing.SwingConstants;

import org.gp.tringes.GestorBD;
import org.gp.tringes.GestorIGDatosDueno;
import org.gp.tringes.GestorIGInicio;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;


public class IGDueno extends JDialog {

	private JPanel panel;
	private JLabel lblZonaPers;
	private JPanel panel_1;
	private JButton btnGesPerros;
	private JButton btnVentasPend;
	private JButton btnComprasPend;
	private JPanel panel_2;
	private JButton btnMostrarDatos;
	private JButton btnDesconectar;
	private String numFederado;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					IGDueno dialog = new IGDueno("");
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
					dialog.setModal(true);
					dialog.setResizable(false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the dialog.
	 */
	public IGDueno(String pNumFederado) {
		numFederado = pNumFederado;
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 400, 250);
		//ID-130
		setTitle(GestorIGInicio.getGestorIGInicio().getTexto(130));
		getContentPane().setLayout(new BorderLayout(0, 0));
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getPanel_1(), BorderLayout.CENTER);
		getContentPane().add(getPanel_2(), BorderLayout.SOUTH);
		
	}
	
	private JPanel getPanel(){
		if(panel == null){
			panel = new JPanel();
			panel.setLayout(new GridLayout(1, 2, 0, 0));
			panel.add(getLblZonaPers());
		}
		return panel;
	}
	private JLabel getLblZonaPers(){
		if(lblZonaPers == null){
			//ID-130
			lblZonaPers = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(130));
			lblZonaPers.setHorizontalAlignment(SwingConstants.CENTER);
			lblZonaPers.setFont(new Font("Bastion", Font.PLAIN, 30));
			lblZonaPers.setBounds(15, 15, 290, 45);
		}
		return lblZonaPers;
	}
	
	private JPanel getPanel_1(){
		if(panel_1 == null){
			panel_1 = new JPanel();
			panel_1.setLayout(new GridLayout(0, 1, 0, 5));
			panel_1.add(getBtnGesPerros());
			panel_1.add(getBtnVentasPend());
			panel_1.add(getBtnComprasPend());
		}
		return panel_1;
	}
	private JButton getBtnGesPerros(){
		if(btnGesPerros == null){
			//ID-78
			btnGesPerros = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(78));
			btnGesPerros.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					setVisible(false);
					JDialog espera = new IGEspera();
					espera.setLocationRelativeTo(null);
					espera.setVisible(true);
					JDialog gesPerros = new IGGestionarPerros(numFederado);
					gesPerros.setResizable(false);
					gesPerros.setModal(true);
					gesPerros.setLocationRelativeTo(null);
					espera.dispose();
			        gesPerros.setVisible(true);
			        setVisible(true);
				}
			});
			btnGesPerros.setBounds(165, 75, 115, 25);
		}
		return btnGesPerros;
	}
	private JButton getBtnVentasPend(){
		if(btnVentasPend == null){
			//ID-131
			btnVentasPend = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(131));
			btnVentasPend.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					setVisible(false);
					JDialog espera = new IGEspera();
					espera.setLocationRelativeTo(null);
					espera.setVisible(true);
					JDialog ventasPend = new IGVentasPend(numFederado);
					ventasPend.setResizable(false);
					ventasPend.setModal(true);
					ventasPend.setLocationRelativeTo(null);
					espera.dispose();
					ventasPend.setVisible(true);
			        setVisible(true);
				}
			});
			btnVentasPend.setBounds(165, 75, 115, 25);
		}
		return btnVentasPend;
	}
	private JButton getBtnComprasPend(){
		if(btnComprasPend == null){
			//ID-82
			btnComprasPend = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(82));
			btnComprasPend.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					setVisible(false);
					JDialog espera = new IGEspera();
					espera.setLocationRelativeTo(null);
					espera.setVisible(true);
					JDialog comprasPend = new IGComprasPend(numFederado);
					comprasPend.setResizable(false);
					comprasPend.setModal(true);
					comprasPend.setLocationRelativeTo(null);
					espera.dispose();
					comprasPend.setVisible(true);
			        setVisible(true);
				}
			});
			btnComprasPend.setBounds(165, 75, 115, 25);
		}
		return btnComprasPend;
	}
	
	private JPanel getPanel_2(){
		if(panel_2 == null){
			panel_2 = new JPanel();
			panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
			panel_2.add(getBtnMostrarDatos());
			panel_2.add(getBtnDesconectar());
		}
		return panel_2;
	}
	private JButton getBtnMostrarDatos(){
		if(btnMostrarDatos == null){
			//ID-132
			btnMostrarDatos = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(132));
			btnMostrarDatos.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					try{
						setVisible(false);
						JDialog espera = new IGEspera();
						espera.setLocationRelativeTo(null);
						espera.setVisible(true);
						JDialog datosDueno = new IGDatosDueno(numFederado);
						datosDueno.setResizable(false);
						datosDueno.setModal(true);
						datosDueno.setLocationRelativeTo(null);
						espera.dispose();
						datosDueno.setVisible(true);
					
						Boolean desactivado = GestorIGDatosDueno.getGestorIGDatosDueno().estaDesactivado(numFederado);
						if(desactivado){
							btnGesPerros.setEnabled(false);
							btnVentasPend.setEnabled(false);
							btnComprasPend.setEnabled(false);
						}
						
						setVisible(true);
					} catch (SQLException es) {
						// TODO Auto-generated catch block
						es.printStackTrace();
					}
				}
			});
			btnMostrarDatos.setBounds(30, 75, 100, 25);
		}
		return btnMostrarDatos;
	}
	private JButton getBtnDesconectar(){
		if(btnDesconectar == null){
			//ID-14
			btnDesconectar = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(14));
			btnDesconectar.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					//ID-15 y 4
					int opcion = JOptionPane.showConfirmDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(15), GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.YES_NO_OPTION);
					if(opcion == 0){
						try {
							GestorBD.getInstance().CerrarConexion();
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						System.exit(0);
					}
				}
			});
		}
		return btnDesconectar;
	}
}